import utils
import string;
import logging;

class WebPresentation:

	def __init__(self):
		pass

	def getMediaList(self, db, sha256Hash = None, mediaTypeLimit = None, fileTypeLimit = None, search = None, tagList = None, secureDownloadKey = None, thumbSize = 3):
		if tagList == "--allTags":
			tagList = None
		elif tagList != "--noTags" and tagList is not None:
			tagList = string.split(tagList, "|")
		mediaList = []
		for details in db.mediaInfo(sha256Hash = sha256Hash, mediaType = mediaTypeLimit, fileType = fileTypeLimit, search = search, tagLimitList = tagList):
			fqpn = "/files/" + utils.getRelativeMediaFile(details)

			if secureDownloadKey is not None:
				relpath = "/f/" + utils.getSecureDownloadKey(utils.getRelativeMediaFile(details), secureDownloadKey)
			else:
				relpath = utils.getRelativeMediaFile(details)
			
			thumbnailPath = ""
			thumbNameDetails = db.getThumbnail(details["sha256Hash"], thumbSize)
			if thumbNameDetails is not None:
				if secureDownloadKey is not None:
					thumbnailPath = "/f/" + utils.getSecureDownloadKey(utils.getRelativeMediaFile(thumbNameDetails), secureDownloadKey)
				else:
					thumbnailPath = utils.getRelativeMediaFile(thumbNameDetails)
			mediaList.append({
				"filename": details["filename"], 
				"relpath": relpath,
				"mediaType": details["mediaType"],
				"fileType": details["fileType"],
				"tags": details["tagList"],
				"sha256Hash": details["sha256Hash"],
				"descr": details["descr"],
				"rating": details["rating"],
				"filesize": details["filesize"],
				"directory": details["directory"],
				"commitDate": details["commitDate"],
				"atime": details["atime"],
				"mtime": details["mtime"],
				"chtime": details["chtime"],
				"s3": details["s3"],
				"thumbnailPath":  thumbnailPath
			})
		return mediaList

	def showVideo(self, db, sha256Hash):
		details = db.mediaInfo(sha256Hash = sha256Hash)[0]
		fqpn = "/files/" + utils.getRelativeMediaFile(details)
		return fqpn

	def showImage(self, db, sha256Hash):
		fqpn = db.getWebPic(sha256Hash)
		link = "/pd/image?sha256Hash={}".format(sha256Hash)
		return link
